package com.xywy.bloodandroid.fragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.google.gson.reflect.TypeToken;
import com.xywy.bloodandroid.R;
import com.xywy.bloodandroid.bean.DoctorSchedule;
import com.xywy.bloodandroid.util.Constants;
import com.xywy.bloodandroid.util.JsonUtil;
import com.xywy.bloodandroid.util.LogUtil;
import com.xywy.bloodandroid.util.NetUtil;
import com.xywy.bloodandroid.util.UIUtil;
import com.xywy.bloodandroid.vollery.GetRequest;
import com.xywy.bloodandroid.vollery.OnVolleyResponseListener;
import com.xywy.bloodandroid.vollery.VolleyManager;

public class VisitingScheduleFraement extends Fragment implements
		OnClickListener {
	protected static final int DOCTORSCHEDULERROR = -1;
	protected static final int DOCTORSCHEDULSUCESS = -99;
	protected ProgressDialog waittingDialog;
	private List<DoctorSchedule> list;
	private TextView one_one;
	private TextView one_two;
	private TextView one_three;
	private TextView two_one;
	private TextView two_two;
	private TextView two_three;
	private TextView three_one;
	private TextView three_two;
	private TextView three_three;
	private TextView four_one;
	private TextView four_two;
	private TextView four_three;
	private TextView five_one;
	private TextView five_two;
	private TextView five_three;
	private TextView six_one;
	private TextView six_two;
	private TextView six_three;
	private TextView seven_one;
	private TextView seven_two;
	private TextView seven_three;
	private HashMap<Integer, String> map = null;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragement_visit_schedule, null);
		initView(view);
		initListener();
		initdata();
		return view;

	}

	private void initdata() {
		list = new ArrayList<DoctorSchedule>();
		Bundle b = this.getArguments();
//		String id = getArguments().getString("expertid");// 获取专家的id
		
		//	此处可能是用的是  doctor_id
		String id=getArguments().getString("doc_id");// 获取专家的id
		System.out.println("获取的专家的id。。。" + id);
		getDoctorTimeManagement(id);
	}

	private String hosptail_address2;
	/**
	 * 获取专家的门诊预约
	 * 
	 * @param id
	 */
	private void getDoctorTimeManagement(String id) {

		final Message msg = Message.obtain();
		GetRequest<String> getRequest = new GetRequest<String>(Constants.getUrl(
				"app", "doctor") + "&source=xywy_app" + "&id=" + id,
				String.class, new OnVolleyResponseListener<String>() {


					@Override
					public void onError(VolleyError volleyError) {
						LogUtil.i(".....Error.......", volleyError + "");

						msg.what = DOCTORSCHEDULERROR;
						handler.sendMessage(msg);
					}

					@Override
					public void onSuccess(String response) {
						UIUtil.DissDialog(getActivity(), waittingDialog);
						LogUtil.i(".....sucess.......", response);
						JSONObject object;
						try {
							object = new JSONObject(response);
							String msg1 = object.optString("msg");
							int result = object.optInt("result");
							JSONObject object2 = object.getJSONObject("data");
							if (result == 1) {
								ParseDoctorScheduleJson(object2);
								
								hosptail_address2 = object2.optString("address");
								msg.what = DOCTORSCHEDULSUCESS;
								handler.sendMessage(msg);

							} else {
								UIUtil.showToast(getActivity(), msg1);
							}
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}
				});

		getRequest.setIsParseJson(false);
		if (NetUtil.hasNetwork(getActivity())) {
			waitingDialog(getActivity(), getActivity().getResources()
					.getString(R.string.request));
			VolleyManager.addRequest(getRequest, getActivity());
		} else {
			UIUtil.showToast(getActivity(), getActivity().getResources()
					.getString(R.string.not_network));
			UIUtil.DissDialog(getActivity(), waittingDialog);
		}
	}

	protected void ParseDoctorScheduleJson(JSONObject object2) {
		String hosptail = object2.optString("hospital");// 医院地址

		JSONObject schedule = object2.optJSONObject("schedule");
		if (schedule != null) {
			String rdtime = schedule.optString("rdtime");
			list.addAll((List<DoctorSchedule>) JsonUtil.parseJsonToList(rdtime,
					new TypeToken<List<DoctorSchedule>>() {
					}.getType()));
		}
	}

	public void waitingDialog(Context context, String message) {
		if (waittingDialog == null)
			waittingDialog = new ProgressDialog(context);
		waittingDialog.setCanceledOnTouchOutside(false);//
		waittingDialog.setMessage(message);
		waittingDialog.show();
	}

	private void initListener() {

	}

	private void initView(View view) {
		
		
		hosptail_address = (TextView) view.findViewById(R.id.hosptail_address);

		one_one = (TextView) view.findViewById(R.id.one_one);
		one_two = (TextView) view.findViewById(R.id.one_two);
		one_three = (TextView) view.findViewById(R.id.one_three);

		two_one = (TextView) view.findViewById(R.id.two_one);
		two_two = (TextView) view.findViewById(R.id.two_two);
		two_three = (TextView) view.findViewById(R.id.two_three);

		three_one = (TextView) view.findViewById(R.id.three_one);
		three_two = (TextView) view.findViewById(R.id.three_two);
		three_three = (TextView) view.findViewById(R.id.three_three);

		four_one = (TextView) view.findViewById(R.id.four_one);
		four_two = (TextView) view.findViewById(R.id.four_two);
		four_three = (TextView) view.findViewById(R.id.four_three);

		five_one = (TextView) view.findViewById(R.id.five_one);
		five_two = (TextView) view.findViewById(R.id.five_two);
		five_three = (TextView) view.findViewById(R.id.five_three);

		six_one = (TextView) view.findViewById(R.id.six_one);
		six_two = (TextView) view.findViewById(R.id.six_two);
		six_three = (TextView) view.findViewById(R.id.six_three);

		seven_one = (TextView) view.findViewById(R.id.seven_one);
		seven_two = (TextView) view.findViewById(R.id.seven_two);
		seven_three = (TextView) view.findViewById(R.id.seven_three);

	}

	public Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case DOCTORSCHEDULERROR:
				UIUtil.DissDialog(getActivity(), waittingDialog);
				UIUtil.showToast(getActivity(), getActivity().getResources()
						.getString(R.string.network_slow));
				break;
			case DOCTORSCHEDULSUCESS:
				
				
				hosptail_address.setText(hosptail_address2);	
				
				
				
				

				for (int i = 0; i < list.size(); i++) {

					String type = list.get(i).getType();
					if (Integer.valueOf(list.get(i).getWeek()) == 1) {
						if (Integer.valueOf(list.get(i).getHalfday()) == 1) {
                             setBackGround(one_one);
                             setText(one_one, changtype(type));
						}
						if (Integer.valueOf(list.get(i).getHalfday()) == 2) {
							setBackGround(one_two);
                            setText(one_two, changtype(type));
						}
						if (Integer.valueOf(list.get(i).getHalfday()) == 3) {
							setBackGround(one_three);
                            setText(one_three, changtype(type));
						}

					} else if (Integer.valueOf(list.get(i).getWeek()) == 2) {
						if (Integer.valueOf(list.get(i).getHalfday()) == 1) {
							setBackGround(two_one);
                            setText(two_one, changtype(type));
						}
						if (Integer.valueOf(list.get(i).getHalfday()) == 2) {
							setBackGround(two_two);
                            setText(two_two, changtype(type));
						}
						if (Integer.valueOf(list.get(i).getHalfday()) == 3) {
							setBackGround(two_three);
                            setText(two_three, changtype(type));
						}

					} else if (Integer.valueOf(list.get(i).getWeek()) == 3) {
						if (Integer.valueOf(list.get(i).getHalfday()) == 1) {
							setBackGround(three_one);
                            setText(three_one, changtype(type));
						}
						if (Integer.valueOf(list.get(i).getHalfday()) == 2) {
							setBackGround(three_two);
                            setText(three_two, changtype(type));
						}
						if (Integer.valueOf(list.get(i).getHalfday()) == 3) {
							setBackGround(three_three);
                            setText(three_three, changtype(type));
						}

					} else if (Integer.valueOf(list.get(i).getWeek()) == 4) {
						if (Integer.valueOf(list.get(i).getHalfday()) == 1) {
							setBackGround(four_one);
                            setText(four_one, changtype(type));
						}
						if (Integer.valueOf(list.get(i).getHalfday()) == 2) {
							setBackGround(four_two);
                            setText(four_two, changtype(type));
						}
						if (Integer.valueOf(list.get(i).getHalfday()) == 3) {
							setBackGround(four_three);
                            setText(four_three, changtype(type));
						}

					} else if (Integer.valueOf(list.get(i).getWeek()) == 5) {
						if (Integer.valueOf(list.get(i).getHalfday()) == 1) {
							setBackGround(five_one);
                            setText(five_one, changtype(type));
						}
						if (Integer.valueOf(list.get(i).getHalfday()) == 2) {
							setBackGround(five_two);
                            setText(five_two, changtype(type));
						}
						if (Integer.valueOf(list.get(i).getHalfday()) == 3) {
							setBackGround(five_three);
                            setText(five_three, changtype(type));
						}

					} else if (Integer.valueOf(list.get(i).getWeek()) == 6) {
						if (Integer.valueOf(list.get(i).getHalfday()) == 1) {
							setBackGround(six_one);
                            setText(six_one, changtype(type));
						}
						if (Integer.valueOf(list.get(i).getHalfday()) == 2) {
							setBackGround(six_two);
                            setText(six_two, changtype(type));
						}
						if (Integer.valueOf(list.get(i).getHalfday()) == 3) {
							setBackGround(six_three);
                            setText(six_three, changtype(type));
						}

					} else if (Integer.valueOf(list.get(i).getWeek()) == 7) {
						if (Integer.valueOf(list.get(i).getHalfday()) == 1) {
							setBackGround(seven_one);
                            setText(seven_one, changtype(type));
						}
						if (Integer.valueOf(list.get(i).getHalfday()) == 2) {
							setBackGround(seven_two);
                            setText(seven_two, changtype(type));
						}
						if (Integer.valueOf(list.get(i).getHalfday()) == 3) {
							setBackGround(seven_three);
                            setText(seven_three, changtype(type));
						}

					}

				}

				break;

			default:
				break;
			}
		}
	};
	private TextView hosptail_address;

	private String changtype(String type) {

		// 门诊类型 (1:普通 2：专家 3：特需 4：专科 5：会诊 6：夜间)

		if (map == null) {
			map = new HashMap<Integer, String>();
			map.put(1, "普通");
			map.put(2, "专家");
			map.put(3, "特需");
			map.put(4, "专科");
			map.put(5, "会诊");
			map.put(6, "夜间");
		}
		Iterator it = map.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry entry = (Map.Entry) it.next();
			Integer key = (Integer) entry.getKey();
			String value = (String) entry.getValue();
			if (Integer.valueOf(type) == key) {
				return value;
			} 
		}
		return "";

	}
	
	private  void setBackGround(TextView  view){
		view.setBackgroundResource(R.color.schedule_color);
	}
	
	private  void  setText(TextView  view,String text){
		view.setText(text);
	}

	@Override
	public void onClick(View v) {

	}
	
	
	 @Override
     public void onDestroy() {
	super.onDestroy();
	VolleyManager.cancelRequest(getActivity());
    }
}
